// pages/home/home.js
import http from '../../http/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topheight: getApp().globalData.topheight,
    beecount: 0,
    beeboxcount: 0,
    warmcount: 0,
    userinfo: '',
    alertpop:false
  },
  onShow:function(){
    this.getUserInfo()
    this.getBeeCount()
    this.getBoxBee()
    this.getWarnings()
  },  
  // 预警
  warning: function() {
    wx.navigateTo({
      url: '../warning/warninginfo',
    })
  },

  toApiaryList: function() {
    wx.navigateTo({
      url: '/pages/myapiary/list/myapiary',
    })
  },
  toBeeBoxList: function() {
    wx.navigateTo({
      url: '/pages/myapiary/details/details?showFc=false',
    })
  },
  // 搜索
  search: function() {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  // 扫码
  scan: function() {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['barCode', 'qrCode'],
      success(res) {
        let codedata = res.result
        console.log(codedata)
        if (codedata.indexOf('http://zn.tianfuhoney.com')==0){
          let coderesult = codedata.substring(codedata.lastIndexOf('=') + 1)
          wx.setStorage({
            key: "code",
            data: coderesult
          })
          http.getdata('/api/beehive/get?deviceCode=' + coderesult).then((res) => {
            wx.navigateTo({
              url: '../beebox/detail/index?deviceCode=' + coderesult,
            })
          })
        }else{
          wx.navigateTo({
            url: '../beebox/add/addbeebox?type=scan&showtoast='+true,
          })
        }
      },
    })
  },
  toApiary: function() {
    wx.navigateTo({
      url: '../myapiary/list/myapiary?myapiary='+true,
    })
  },
  // 获取蜂c数量
  getBeeCount: function() {
    wx.hideLoading()
    http.getdata('/api/apiary/count').then((res) => {
      this.setData({
        beecount: res.data.result
      })
    })
  },
  // 获取蜂箱数量
  getBoxBee: function() {
    http.getdata('/api/beehive/count').then((res) => {
      this.setData({
        beeboxcount: res.data.result
      })
    })
  },
  //警告数量
  getWarnings: function()/*  */ {
    http.getdata('/api/monitor/count').then((res) => {
      this.setData({
        warmcount: res.data.result
      })
    })
  },
  // 用户信息
  getUserInfo: function() {
    http.getdata('/api/userinfo/get').then((res) => {
      this.setData({
        userinfo: res.data.result
      })
    })
  },
  // 登出
  showPop: function() {
    this.setData({
      alertpop: true
    })
  },
  closePop:function(){
    this.setData({
      alertpop:false
    })
  },
  loginOut:function(){
    this.setData({
      alertpop: false
    })
    http.postdata('/sys/logout').then((res) => {
      wx.redirectTo({
        url: '../login/login',
        success: function () {
          wx.clearStorage()
        }
      })
    })
  }
})
